package com.code.petapp.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.Toast;

import com.code.petapp.activity.BuyActivity;
import com.code.petapp.adapter.ShopAdapter;
import com.code.petapp.bean.AnimalBean;
import com.code.petapp.bean.GoodsBean;
import com.code.petapp.databinding.FragmentShopBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.AnimalUtils;
import com.code.petapp.util.GoodsUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.annotations.Nullable;

public class ShopFragment extends Fragment implements ShopAdapter.OnItemClickListener {
    private RecyclerView recyclerView;//声明RecyclerView
    private ShopAdapter shopAdapter;
    private boolean isGetData = false;
    FragmentShopBinding binding;
    private List<GoodsBean> list;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentShopBinding.inflate(inflater, container, false);
        View view = binding.getRoot();
        list = new ArrayList<>();
        load("");
        search();
        recyclerView=binding.recView;
        shopAdapter=new ShopAdapter(getContext(),list);
        shopAdapter.setOnItemClickListener(this);
        GridLayoutManager manager = new GridLayoutManager(getContext(),2);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(shopAdapter);
        return view;
    }
    @Override
    public void onResume() {//每次重进页面都会刷新数据
        if (!isGetData) {
            //   这里可以做网络请求或者需要的数据刷新操作
            load("");
            isGetData = true;
        }
        super.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        isGetData = false;
    }
    private void search(){
        binding.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                searchData(s);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                searchData(s);
                return false;
            }
        });
    }
    private void searchData(String s){
        load(s);
    }
    private void load(String name){
        GoodsUtils.selectAll(SPCommonUtils.getInstance().getToken(), name, new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(@Nullable ArrayList<GoodsBean> beanList) {
                list.clear();
                list.addAll(beanList);
                shopAdapter.notifyDataSetChanged();
            }
            @Override
            public void error(int code, @Nullable String error) {
                Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View parent, int position) {
        int goodsId=list.get(position).getId();
        String price=list.get(position).getPrice();
        int num=list.get(position).getNum();
        Intent intent=new Intent(getActivity(), BuyActivity.class);
        intent.putExtra("goodsId",goodsId);
        intent.putExtra("price",price);
        intent.putExtra("num",num);
        startActivity(intent);

    }
}